# -*- coding : utf-8 -*-
# coding: utf-8
"""
给算法/分析模型python程序的演示微服务接口demo，这是运行主程序
"""


import logging.config
from flask import Flask
from flask_cors import CORS
# 自定义模块
from config import config_dict


def create_app(config_name):
    app = Flask(__name__)
    CORS(app)  # 允许所有来源的跨域请求

    app.config.from_object(config_dict[config_name])
    app.debug = app.config['DEBUG']

    from api import api_bp
    #访问api蓝图的路径
    app.register_blueprint(api_bp, url_prefix='/V1.0/xidian/manufacturedy')

    return app


# 根据配置文件初始化日志工具
logging.config.fileConfig('logging.conf')
APP_ENV = "production"  # development | production | testing

# main() entrypoint
if __name__ == "__main__":
    root_logger = logging.getLogger('root')
    logger = logging.getLogger('main')

    # 0. 读取全局配置作为默认值
    app = create_app(APP_ENV)
    ipAddr = app.config['IPADDR']

    portNum = app.config['PORT']

    # 1. 接收外部参数,
    # ipAddr = '0.0.0.0'     # 未来可通过argparse -d 输入参数修改默认值
    # portNum = 5555     # 未来可通过argparse -d 输入参数修改默认值

    logger.info('==================== 开启服务  ====================')
    # app.config['JSON_AS_ASCII'] = False
    app.config.update(RESTFUL_JSON=dict(ensure_ascii=False))
    app.run(host=ipAddr, port=portNum, debug=app.config['DEBUG'])

